﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class XmlPropertyCollection : IXmlPropertyCollection
    {
        int index = -1;

        List<XmlProperty> list = new List<XmlProperty>();

        public IXmlProperty this[int index]
        {
            get
            {
                if (index < 0 || index > list.Count - 1)
                    throw new Exception("index exceed list.count = " + list.Count);

                return list[index];
            }
        }

        public string this[string propertyName]
        {
            get
            {
                if (string.IsNullOrEmpty(propertyName))
                    return null;

                propertyName = propertyName.Trim();

                foreach (XmlProperty property in list)
                {
                    if (string.Equals(property.Key, propertyName, StringComparison.OrdinalIgnoreCase))
                        return property.Value;
                }

                return null;
            }
            set
            {
                if (string.IsNullOrEmpty(propertyName))
                    return;

                propertyName = propertyName.Trim();

                foreach (XmlProperty property in list)
                {
                    if (string.Equals(property.Key, propertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (string.IsNullOrEmpty(value))
                            property.Value = null;
                        else
                            property.Value = value.Trim();

                        return;
                    }
                }

                //add new one

                this.Add(propertyName, value);
            }
        }

        public IXmlProperty Get(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                return null;

            foreach (XmlProperty property in list)
            {
                if (string.Equals(property.Key, propertyName, StringComparison.OrdinalIgnoreCase))
                {
                    return property;
                }
            }

            return null;
        }

        public T Get<T>(string propertyName)
        {
            return Get<T>(propertyName, null);
        }

        public T Get<T>(string propertyName, string format)
        {
            string value = this[propertyName];

            T rvalue = default(T);

            if (string.IsNullOrEmpty(value))
                return rvalue;

            Pixysoft.Tools.ParserHelper.TryParse<T>(value, out rvalue, format);

            return rvalue;
        }

        public IXmlProperty Add(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
                throw new Exception("missing key for property.");
            else
                key = key.Trim();

            if (string.IsNullOrEmpty(value))
                value = null;
            else
                value = value.Trim();

            XmlProperty property = new XmlProperty(key, value);

            list.Add(property);

            return property;
        }

        public IXmlProperty Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;

            XmlProperty value = Get(key) as XmlProperty;

            if (value == null)
                return value;

            list.Remove(value);

            return value;
        }

        public bool Contains(string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            key = key.Trim();

            foreach (XmlProperty property in list)
            {
                if (string.Equals(property.Key, key, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public void Clear()
        {
            list.Clear();
        }

        public int Count
        {
            get { return list.Count; }
        }

        #region IEnumerable<IXmlProperty> 成员

        public IEnumerator<IXmlProperty> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IXmlProperty> 成员

        public IXmlProperty Current
        {
            get
            {
                return this[index];
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this[index];
            }
        }

        public bool MoveNext()
        {
            if (index >= list.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        public override string ToString()
        {
            return "property.count = " + list.Count;
        }

    }
}
